<?php

namespace Acme\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

use Symfony\Component\Validator\Constraints as Assert;

// DON'T forget this use statement!!!
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

use Symfony\Component\Security\Core\User\UserInterface;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
 * Acme\UserBundle\Entity\CmUser
 *
 * @ORM\Table(name="cm_user")
 * @ORM\Entity(repositoryClass="Acme\UserBundle\Repository\UserRepository")
 * @ORM\HasLifecycleCallbacks()
 * 
 * @UniqueEntity(
 * 		fields="username",
 * 		message="该用户名已被使用",
 * 		groups={"regist_default"}
 * )
 * @UniqueEntity(
 * 		fields="email",
 * 		message="该邮箱已被使用",
 * 		groups={"regist_default"}
 * )
 */
class CmUser implements UserInterface, \Serializable
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string $username
     *
     * @ORM\Column(name="username", type="string", length=255, nullable=true)
     * 
     * @Assert\NotBlank(
     * 		message = "请输入用户名",
     * 		groups={"regist_default"}
     * )
     * @Assert\MinLength(
     * 		limit=4,
     * 		message="用户名长度最少为 {{ limit }} 位字符",
     * 		groups={"regist_default"}
     * )
     * @Assert\MaxLength(
     * 		limit=20,
     * 		message="用户名长度最多为 {{ limit }} 位字符",
     * 		groups={"regist_default"}
     * )
     * @Assert\Regex(
     * 		pattern="/[a-zA-Z0-9_-]+$/",
     * 		message="用户名只能由英文、数字及“_”、“-”组成",
     * 		groups={"regist_default"}
     * )
     */
    private $username;
    
    /**
     * @ORM\Column(name="salt", type="string", length=255, nullable=true)
     */
    private $salt;

    /**
     * @var string $password
     *
     * @ORM\Column(name="password", type="string", length=255, nullable=true)
     * 
     * @Assert\NotBlank(
     * 		message = "请输入密码",
     * 		groups={"regist_default"}
     * )
     * @Assert\MinLength(
     * 		limit=6,
     * 		message="密码长度最少为 {{ limit }} 位字符",
     * 		groups={"regist_default"}
     * )
     * @Assert\MaxLength(
     * 		limit=16,
     * 		message="密码长度最多为 {{ limit }} 位字符",
     * 		groups={"regist_default"}
     * )
     * @Assert\Regex(
     * 		pattern="/[a-zA-Z0-9_-]+$/",
     * 		message="密码只能由英文、数字及“_”、“-”组成",
     * 		groups={"regist_default"}
     * )
     */
    private $password;

    /**
     * @var string $email
     *
     * @ORM\Column(name="email", type="string", length=255, nullable=true)
     * 
     * @Assert\NotBlank(
     * 		message = "请输入邮箱",
     * 		groups={"regist_default"}
     * )
     * @Assert\Email(
     * 		message = "邮箱格式不正确",
     * 		
     * 		groups={"regist_default"}
     * )
     * checkMX = true,
     */
    private $email;

    /**
     * @var string $category
     *
     * @ORM\Column(name="category", type="string", length=1, nullable=true)
     */
    private $category;

    /**
     * @var integer $createdBy
     *
     * @ORM\Column(name="created_by", type="integer", nullable=true)
     */
    private $createdBy;

    /**
     * @var datetime $createdTime
     *
     * @ORM\Column(name="created_time", type="datetime", nullable=true)
     */
    private $createdTime;

    /**
     * @var integer $approver
     *
     * @ORM\Column(name="approver", type="integer", nullable=true)
     */
    private $approver;

    /**
     * @var datetime $approveTime
     *
     * @ORM\Column(name="approve_time", type="datetime", nullable=true)
     */
    private $approveTime;

    /**
     * @var integer $modifiedBy
     *
     * @ORM\Column(name="modified_by", type="integer", nullable=true)
     */
    private $modifiedBy;

    /**
     * @var datetime $modifiedTime
     *
     * @ORM\Column(name="modified_time", type="datetime", nullable=true)
     */
    private $modifiedTime;

    /**
     * @var string $userStatus
     *
     * @ORM\Column(name="user_status", type="string", length=1, nullable=true)
     */
    private $userStatus;
    
    /**
     * @var string $emailStatus
     *
     * @ORM\Column(name="email_status", type="string", length=1, nullable=true)
     */
    private $emailStatus;
    
    /**
     * @ORM\Column(name="is_active", type="boolean")
     */
    private $isActive;
    
    /**
     * @var string $flag
     *
     * @ORM\Column(name="flag", type="string", length=1, nullable=true)
     */
    private $flag;
    
    /**
     * Inverse Side
     * @ORM\OneToOne(targetEntity="CmUserInfo", mappedBy="user")
     */
    protected $userInfo;
    
    /**
     * Inverse Side
     * @ORM\OneToMany(targetEntity="\Acme\ContestBundle\Entity\CtInfo", mappedBy="applyByInfo")
     */
    protected $applyContests;
    
    public function __construct()
    {
    	$this->applyContests = new ArrayCollection();
    	
    	/**
    	 * code for password encoder
    	 */
    	$this->isActive = true;
    	$this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
    }


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set username
     *
     * @param string $username
     */
    public function setUsername($username)
    {
        $this->username = $username;
    }

    /**
     * Get username
     *
     * @return string 
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * Set password
     *
     * @param string $password
     */
    public function setPassword($password)
    {
        $this->password = $password;
    }

    /**
     * Get password
     *
     * @return string 
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set email
     *
     * @param string $email
     */
    public function setEmail($email)
    {
        $this->email = $email;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set category
     *
     * @param string $category
     */
    public function setCategory($category)
    {
        $this->category = $category;
    }

    /**
     * Get category
     *
     * @return string 
     */
    public function getCategory()
    {
        return $this->category;
    }

    /**
     * Set createdBy
     *
     * @param integer $createdBy
     */
    public function setCreatedBy($createdBy)
    {
        $this->createdBy = $createdBy;
    }

    /**
     * Get createdBy
     *
     * @return integer 
     */
    public function getCreatedBy()
    {
        return $this->createdBy;
    }

    /**
     * Set createdTime
     *
     * @param datetime $createdTime
     * 
     * @ORM\prePersist
     */
    public function setCreatedTime()
    {
        $this->createdTime = new \DateTime();
    }

    /**
     * Get createdTime
     *
     * @return datetime 
     */
    public function getCreatedTime()
    {
        return $this->createdTime;
    }

    /**
     * Set approver
     *
     * @param integer $approver
     */
    public function setApprover($approver)
    {
        $this->approver = $approver;
    }

    /**
     * Get approver
     *
     * @return integer 
     */
    public function getApprover()
    {
        return $this->approver;
    }

    /**
     * Set approveTime
     *
     * @param datetime $approveTime
     */
    public function setApproveTime($approveTime)
    {
        $this->approveTime = $approveTime;
    }

    /**
     * Get approveTime
     *
     * @return datetime 
     */
    public function getApproveTime()
    {
        return $this->approveTime;
    }

    /**
     * Set modifiedBy
     *
     * @param integer $modifiedBy
     */
    public function setModifiedBy($modifiedBy)
    {
        $this->modifiedBy = $modifiedBy;
    }

    /**
     * Get modifiedBy
     *
     * @return integer 
     */
    public function getModifiedBy()
    {
        return $this->modifiedBy;
    }

    /**
     * Set modifiedTime
     *
     * @param datetime $modifiedTime
     * 
     * @ORM\preUpdate
     */
    public function setModifiedTime()
    {
        $this->modifiedTime = new \DateTime();
    }

    /**
     * Get modifiedTime
     *
     * @return datetime 
     */
    public function getModifiedTime()
    {
        return $this->modifiedTime;
    }

    /**
     * Set userStatus
     *
     * @param string $userStatus
     * 
     * @ORM\prePersist
     */
    public function setUserStatus($userStatus = 2)
    {
        $this->userStatus = $userStatus;
    }

    /**
     * Get userStatus
     *
     * @return string 
     */
    public function getUserStatus()
    {
        return $this->userStatus;
    }
    
    /**
     * Set emailStatus
     *
     * @param string $emailStatus
     *
     * @ORM\prePersist
     */
    public function setEmailStatus($emailStatus = 0)
    {
    	$this->emailStatus = $emailStatus;
    }
    
    /**
     * Get emailStatus
     *
     * @return string
     */
    public function getEmailStatus()
    {
    	return $this->emailStatus;
    }

    /**
     * Set flag
     *
     * @param string $flag
     * 
     * @ORM\prePersist
     */
    public function setFlag($flag = 0)
    {
        $this->flag = $flag;
    }

    /**
     * Get flag
     *
     * @return string 
     */
    public function getFlag()
    {
        return $this->flag;
    }

    /**
     * Set userInfo
     *
     * @param Acme\UserBundle\Entity\CmUserInfo $userInfo
     */
    public function setUserInfo(\Acme\UserBundle\Entity\CmUserInfo $userInfo)
    {
        $this->userInfo = $userInfo;
    }

    /**
     * Get userInfo
     *
     * @return Acme\UserBundle\Entity\CmUserInfo 
     */
    public function getUserInfo()
    {
        return $this->userInfo;
    }
    
    /**
     * TODO load parameters from app/config.yml just like global variables in Twig Bundle
     * @see Symfony\Component\Security\Core\User.UserInterface::getRoles()
     */
    public function getRoles()
    {
    	/*$container = new ContainerBuilder();
    	$userCategoryEn = $container->get('gender');
    	echo "<pre>";
    	print_r($userCategoryEn);
    	return ;*/
    	$userCategoryEn = array(1 => 'university_admin', 2 => 'student', 3 => 'teacher', 4 => 'outsider');
    	$role = 'ROLE_' . strtoupper($userCategoryEn[$this->category]);
    	return array($role);
    }
    
    public function equals(UserInterface $user)
    {
    	return $user->getUsername() === $this->username;
    }
    
    public function eraseCredentials()
    {
    }
    
    public function getSalt()
    {
    	return $this->salt;
    }
    
    public function serialize()
    {
    	return serialize(array(
    			$this->id,
    			$this->password,
    			$this->username
    	));
    }
    
    public function unserialize($serialized)
    {
    	list(
    			$this->id,
    			$this->password,
    			$this->username
    	) = unserialize($serialized);
    }

    /**
     * Set salt
     *
     * @param string $salt
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;
    }

    /**
     * Set isActive
     *
     * @param boolean $isActive
     */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;
    }

    /**
     * Get isActive
     *
     * @return boolean 
     */
    public function getIsActive()
    {
        return $this->isActive;
    }

    /**
     * Add applyContests
     *
     * @param Acme\ContestBundle\Entity\CtInfo $applyContests
     */
    public function addCtInfo(\Acme\ContestBundle\Entity\CtInfo $applyContests)
    {
        $this->applyContests[] = $applyContests;
    }

    /**
     * Get applyContests
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getApplyContests()
    {
        return $this->applyContests;
    }
}